package com.zcm.modules.zhaocai.scheduler;

import com.zcm.common.config.Global;
import com.zcm.common.utils.CacheUtils;
import com.zcm.modules.xgb.utils.XgbUtils;
import com.zcm.modules.zhaocai.entity.ZcmKanpanData;
import com.zcm.modules.zhaocai.service.ZcmKanpanDataService;
import com.zcm.modules.zhaocai.entity.ZcmKanpanData;
import com.zcm.modules.zhaocai.service.ZcmKanpanDataService;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Lazy(false)
public class ZhaoCaiTaskDataService {
    @Autowired
    ZcmKanpanDataService zcmKanpanDataService;

    @Scheduled(cron = "0/5 * 9-15 ? * MON-FRI")
    public void ZcmTask(){
        kpDataTask();
    }

    @Scheduled(cron = "0 0 18,22 ? * MON-FRI")
    private void kpDataTask() {
        boardStockTask();
        boomStockTask();
        boardDownTask();
        boardPredayTask();
        multiStockTask();
        realTask();
        quoteChangeTask();
    }


    @Scheduled(cron = "0 5 22 ? * MON-FRI")
    private void xgbHisData() {
        // 只留一个master做这个任务。
        if(!Global.TRUE.equals(Global.getConfig("master"))){
            return;
        }
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setDay(String.format("%tF",new Date()));
        List<ZcmKanpanData> list = zcmKanpanDataService.findList(zcmKanpanData);
        for (ZcmKanpanData d:list) {
            XgbUtils.parseXgbData(d);
        }
    }

    public void boardStockTask() {
        String boardStockData = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/v2/sheet/board_stock?filter=true");//一字板和涨停板和连板个股
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(boardStockData);
        zcmKanpanData.setType("boardStockTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void boomStockTask() {
        String boomStockTask = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/v2/sheet/boom_stock");//被砸开板
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(boomStockTask);
        zcmKanpanData.setType("boomStockTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void boardDownTask() {
        String boardDownTask = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/v3/saga/pool/board-down");//跌停板
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(boardDownTask);
        zcmKanpanData.setType("boardDownTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void boardPredayTask() {
        String boardPredayTask = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/v3/saga/pool/board-preday");//昨日涨停
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(boardPredayTask);
        zcmKanpanData.setType("boardPredayTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void multiStockTask() {
        String multiStockTask = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/v2/sheet/multi_stock");//强势股
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(multiStockTask);
        zcmKanpanData.setType("multiStockTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void realTask() {
        String realTask = HttpClientUtil.doGet("https://forexdata.wallstreetcn.com/real?en_prod_code=000001.SS,399001.SZ,399006.SZ&fields=last_px,px_change_rate");//上深创
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        if(!realTask.contains("399001")){
            return;
        }
        zcmKanpanData.setContent(realTask);
        zcmKanpanData.setType("realTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    public void quoteChangeTask() {
        String quoteChangeTask = HttpClientUtil.doGet("https://wows-api.wallstreetcn.com/statis_data/quote_change");//涨跌平
        ZcmKanpanData zcmKanpanData = new ZcmKanpanData();
        zcmKanpanData.setContent(quoteChangeTask);
        zcmKanpanData.setType("quoteChangeTask");
        zcmKanpanData.setCreateDate(new Date());
        zcmKanpanData.setUpdateDate(zcmKanpanData.getCreateDate());
        zcmKanpanData.setDay(String.format("%tF",zcmKanpanData.getCreateDate()));
        saveKpData(zcmKanpanData);
    }

    private void saveKpData( ZcmKanpanData zcmKanpanData){
        if(!Global.TRUE.equals(Global.getConfig("master"))){
            CacheUtils.put(ZcmKanpanDataService.CACHE_TYPE,zcmKanpanData.getType(),zcmKanpanData);
        }else{
            zcmKanpanDataService.save(zcmKanpanData);
        }
    }
    public static void main(String [] args) throws IOException {
        {
            String path = "";//需要压缩的文件路径
            String filename = "";//需要压缩的文件文件名

            File output = new File("aaa.txt");//压缩后文件的存放路径
            SevenZOutputFile sevenZOutputFile = null;
            sevenZOutputFile = new SevenZOutputFile(output);
            File file = new File(path);

            SevenZArchiveEntry entry = sevenZOutputFile.createArchiveEntry(file, filename);
            sevenZOutputFile.putArchiveEntry(entry);
            FileInputStream in = new FileInputStream(file);
            byte[] b = new byte[1024];
            int count = 0;
            while ((count = in.read(b)) > 0) {
//                          System.out.println("count:"+count);
                sevenZOutputFile.write(b, 0, count);
            }
            in.close();
            sevenZOutputFile.closeArchiveEntry();
            sevenZOutputFile.close();

        }

    }
}